package com.hzya.frame.hr.staff.controller; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.hzya.frame.utils.core.action.DefaultController;
import com.hzya.frame.utils.core.entity.JsonResultEntity;
import com.hzya.frame.utils.core.page.PageInfo;
import com.hzya.frame.hr.staff.entity.StaffEntity;
import com.hzya.frame.hr.staff.service.IStaffService;
 
/**
 * @description: 员工管理  controller
 * @tableName: hr_staff 
 * @entityName: StaffEntity
 * 包含：
 * 1、分页查询(queryPaged)
 * 2、index页面(goIndexPage)
 * 3、新增页面(goAddPage)
 * 4、新增信息保存(save)
 * 5、修改页面(goEditPage)
 * 6、修改信息保存(update)
 * 7、信息查看(goShowPage)
 * 8、删除信息(delete)
 * @author: gjh
 * @history: 1.0
 */

@Controller("hr_staff")
@RequestMapping("/hr/staff/StaffController")
public class StaffController extends DefaultController {
    @Autowired
    protected IStaffService  staffService;

    /** 员工管理分页信息 **/
    @RequestMapping(value = "queryPaged")
    @ResponseBody
    public PageInfo<StaffEntity> queryPaged( StaffEntity entity) throws Exception {
		PageInfo<StaffEntity> page =null;
		try {
		   page = staffService.queryPaged(entity);
		} catch (Exception e) {
		    e.printStackTrace();
		}
		return page;
    }

	/** 跳转到员工管理的index页面 **/
    @RequestMapping(value = "goIndexPage", method = RequestMethod.GET)
    public ModelAndView goIndexPage() {
		ModelAndView view = new ModelAndView();
		/**这里根据实际的需要填写需要传到前台的对象**/
		//view.addObject(attributeName, attributeValue);
		/**这里是返回对应的页面路径**/
		view.setViewName("/hr/staff/staff/list");
		return view;
    }

	/** 跳转到员工管理信息新增页面 **/
    @RequestMapping(value = "goAddPage")
    public ModelAndView goAddPage() {
		ModelAndView view = new ModelAndView();
		/**这里根据实际的需要填写需要传到前台的对象**/
		//view.addObject(attributeName, attributeValue);
		/**这里是返回对应的页面路径**/
		view.setViewName("/hr/staff/staff/add");
		return view;
    }
    
    /** 员工管理新增信息保存 **/
    @RequestMapping(value = "save", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultEntity save( StaffEntity entity) {
		try {
		    staffService.save(entity);
		    return getSuccessMessageEntity("保存成功", "save");
		} catch (Exception e) {
		    logger.error("save", e);
		    e.printStackTrace();
		    return getFailureMessageEntity("保存失败！"+e.getMessage(), "save");
		}
    }
    
    /** 跳转到员工管理信息修改页面 **/
    @RequestMapping(value = "edit/{id}")
    public ModelAndView goEditPage(@PathVariable("id") Long id) {
		ModelAndView view = new ModelAndView();
		/**这里根据实际的需要填写需要传到前台的对象**/
		StaffEntity entity = staffService.get(id);
		view.addObject("entity", entity);
		/**这里是返回对应的页面路径**/
		view.setViewName("/hr/staff/staff/edit");
		return view;
    }
    
    /** 员工管理修改信息保存 **/
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultEntity update( StaffEntity entity) {
		try {
		    staffService.update(entity);
		    return getSuccessMessageEntity("更新成功！", "update");
		} catch (Exception e) {
		    logger.error("update", e);
		    e.printStackTrace();
		    return getFailureMessageEntity("更新失败！"+e.getMessage(), "update");
		}
    }
    
    /** 跳转到员工管理信息查看 **/
    @RequestMapping(value = "show/{id}")
    public ModelAndView goShowPage(@PathVariable("id") Long id) {
		ModelAndView view = new ModelAndView();
		/**这里根据实际的需要填写需要传到前台的对象**/
		StaffEntity entity = staffService.get(id);
		view.addObject("entity", entity);
		/**这里是返回对应的页面路径**/
		view.setViewName("/hr/staff/staff/show");
		return view;
    }

	/** 删除员工管理信息 **/
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultEntity delete(Long id) {
		try {
		    staffService.logicRemove(id);
		    return getSuccessMessageEntity("删除成功！", "delete");
		} catch (Exception e) {
		    logger.error("delete", e);
		    e.printStackTrace();
		    return getFailureMessageEntity("删除失败！"+e.getMessage(), "delete");
		}
    }

	/**
	 * 去获取员工列表
	 * @return
	 */
	@RequestMapping(value = "goStaffPage")
	public ModelAndView goStaffListPage(String inputID){
		ModelAndView view = new ModelAndView();
		view.addObject("inputID",inputID);
		view.setViewName("hr/staff/staff/staffList");
		return  view;
	}
}

